﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using AssignmentSWEN3.App_Code;

namespace AssignmentSWEN3
{
    public partial class BookingViewNew : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StartTextBox.Text = Convert.ToDateTime(Request.QueryString["start"]).ToShortDateString();
                EndTextBox.Text = Convert.ToDateTime(Request.QueryString["end"]).ToShortDateString();

                NameTextBox.Focus();

                DropDownRoomList.DataSource = dbGetResources();
                DropDownRoomList.DataTextField = "name";
                DropDownRoomList.DataValueField = "id";
                DropDownRoomList.SelectedValue = Request.QueryString["r"];
                DropDownRoomList.DataBind();
            }
        }
       
        private DataTable dbGetResources()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name] FROM [resource]", ConnectionClass.Get(this));
            DataTable dt = new DataTable();
            da.Fill(dt);

            return dt;
        }

        private string dbInsertEvent(DateTime start, DateTime end, string name, string resource, int status, string address, string email, int phone, int noadult, int nochildren, string creditcardno, DateTime creditcardexp)
        {
            string id = Guid.NewGuid().ToString();

            using (SqlConnection con = new SqlConnection(ConnectionClass.Get(this)))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO [event] (id, eventstart, eventend, name, resource_id, status, address, email, phone, Noadult, Nochildren, creditcardno, creditcardexp) VALUES(@id, @start, @end, @name, @resource, @status, @address, @email, @phone, @adult, @children , @creditnumber , @creditexp )", con);
                cmd.Parameters.AddWithValue("id", id);
                cmd.Parameters.AddWithValue("start", start);
                cmd.Parameters.AddWithValue("end", end);
                cmd.Parameters.AddWithValue("name", name);
                cmd.Parameters.AddWithValue("resource", resource);
                cmd.Parameters.AddWithValue("status", status);
                cmd.Parameters.AddWithValue("address", address);
                cmd.Parameters.AddWithValue("phone", phone);
                cmd.Parameters.AddWithValue("email", email);
                cmd.Parameters.AddWithValue("adult", noadult);
                cmd.Parameters.AddWithValue("children", nochildren);
                cmd.Parameters.AddWithValue("creditnumber", creditcardno);
                cmd.Parameters.AddWithValue("creditexp", creditcardexp);
                cmd.ExecuteNonQuery();
            }

            return id;
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            ModalClass.Close(this);
        }

        protected void ButtonOkay_Click(object sender, EventArgs e)
        {
            DateTime start = Convert.ToDateTime(StartTextBox.Text);
            DateTime end = Convert.ToDateTime(EndTextBox.Text);
            string name = NameTextBox.Text;
            string resource = DropDownRoomList.SelectedValue;
            string address = AddressTextBox.Text;
            string email = EmailTextBox.Text;
            int phone = Convert.ToInt32(PhoneTextBox.Text);
            int adult = Convert.ToInt32(TextBoxAdult.Text);
            int child = Convert.ToInt32(TextBoxChild.Text);
            string creditcardno = TextBox1.Text;
            DateTime creditcardexp = Convert.ToDateTime(TextBox2.Text);

            dbInsertEvent(start, end, name, resource, 0, address, email, phone, adult, child, creditcardno, creditcardexp);
            ModalClass.Close(this, "OK");
        }
    }
}